export default {
    /**
     * 给金额添加货币符号
     * @param {金额} value 
     * @param {货币符号} currency 
     */
    addCurrency(value, currency) {
        return currency + value
    },
    /**
     * 格式化时间
     * @param {时间} value 
     * @param {期望格式} formatter yyyy.mm.dd yyyy-mm-dd 等
     */
    formatDate(value, formatter) {
        let time = new Date(value)
        let y = time.getFullYear()
        let m = time.getMonth() + 1
        m = m < 10 ? '0' + m : m
        let d = time.getDate()
        d = d < 10 ? ('0' + d) : d
        let h = time.getHours()
        h = h < 10 ? ('0' + h) : h
        let mi = time.getMinutes()
        mi = mi < 10 ? ('0' + mi) : mi
        let s = time.getSeconds()
        s = s < 10 ? ('0' + s) : s
        if (formatter == 'yyyy.mm.dd') {
            return y + '.' + m + '.' + d
        } else if (formatter == 'yyyy-mm-dd') {
            return y + '-' + m + '-' + d
        } else if (formatter == 'yyyy/mm/dd') {
            return y + '/' + m + '/' + d
        } else if (formatter == 'yyyy-mm-dd hh:mi') {
            return y + '-' + m + '-' + d + ' ' + h + ':' + mi
        }
    },
    /**
     * 格式化时间间隔
     * @param {时间} value 
     */
    formatInterval(value) {
        let time = new Date(value)
        let currentTime = new Date().getTime()
        let diffTime = currentTime - time.getTime()
        let tenMinutes = 10 * 60 * 1000
        let anHour = 60 * 60 * 1000
        let oneDay = 24 * 60 * 60 * 1000
        let oneYear = 365 * 24 * 60 * 60 * 1000
        let y = time.getFullYear()
        let m = time.getMonth() + 1
        m = m < 10 ? '0' + m : m
        let d = time.getDate()
        d = d < 10 ? ('0' + d) : d
        let h = time.getHours()
        h = h < 10 ? ('0' + h) : h
        let mi = time.getMinutes()
        mi = mi < 10 ? ('0' + mi) : mi
        if (diffTime < tenMinutes) {
            return '刚刚'
        }
        if (y != new Date().getFullYear()) {
            return y + '-' + m + '-' + d
        }
        if (diffTime >= tenMinutes && diffTime < anHour) {
            for (let i = 1; i < 6; i++) {
                if (diffTime >= i * tenMinutes && diffTime < (i+1) * tenMinutes) {
                    return i * 10 + '分钟前'
                }
            }
        }
        if (diffTime >= anHour && diffTime < oneDay) {
            for (let i = 1; i < 24; i++) {
                if (diffTime >= i * anHour && diffTime < (i+1) * anHour) {
                    return i + '小时前'
                }
            }
        }
        if (diffTime >= oneDay && diffTime < oneYear) {
            return m + '-' + d + ' ' + h + ':' + mi
        }
    }
}
